package com.app.Cutofftime;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.app.Cutofftime.Database.DatabaseHelper;
import com.google.android.material.snackbar.Snackbar;


public class Timecut1Activity extends AppCompatActivity {

    private EditText  input_title, input_code, input_description, input_date,input_time,input_percentage ;
    private RadioButton generic;
    private RadioGroup input_type;
    private Button addTime;
    private DatabaseHelper helper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timecut1);

        Toolbar toolbar = findViewById(R.id.bar6);
        toolbar.setTitle("Add a time limit for your course");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        input_title   = findViewById(R.id.module_title);
        input_code    = findViewById(R.id.xml_code);
        input_type   = findViewById(R.id.module_type);
        input_description  = findViewById(R.id.xml_description);
        input_date  = findViewById(R.id.xml_date);
        input_time     = findViewById(R.id.xml_time);
        input_percentage   = findViewById(R.id.xml_percentage);



        addTime     = findViewById(R.id.addrecord);
        helper = new DatabaseHelper(getApplicationContext());

        addTime.setOnClickListener(v -> {

            String title  = "",  code = "", type = "", description = "", date = "",  time = "", percentage = "";

            if(input_title.length() == 0  || input_percentage.length() == 0
                    || input_code.length() == 0|| input_description.length() == 0){
                Snackbar.make(input_title, "Please enter all fields", Snackbar.LENGTH_SHORT).show();

            }
            else if (input_date.length()!=10 || input_time.length()!=5 ) {
                Toast.makeText(getApplicationContext(),"Error: Please check your date and time formats!",Toast.LENGTH_SHORT).show();

            }

            else {
                title  = input_title.getText().toString().trim();
                code   = input_code.getText().toString().trim();
                generic    = findViewById(input_type.getCheckedRadioButtonId());
                type = generic.getText().toString();
                description= input_description.getText().toString();
                date   = input_date.getText().toString();
                time    = input_time.getText().toString();
                percentage   = input_percentage.getText().toString();


                if(date.substring(4,5).equals("-")&&date.substring(7,8).equals("-")
                &&isNumeric(date.substring(0,4))==true
                &&isNumeric(date.substring(5,6))==true
                &&isNumeric(date.substring(6,7))==true
                &&isNumeric(date.substring(8,9))==true
                &&isNumeric(date.substring(9,10))==true
                 &&time.substring(2,3).equals(":")
                 &&isNumeric(time.substring(0,1))==true
                 &&isNumeric(time.substring(1,2))==true
                 &&isNumeric(time.substring(3,4))==true
                 &&isNumeric(time.substring(4,5))==true)
                {


                    if(helper.addTimelimit(title, code , type, description, date, time,percentage)){
                        Snackbar.make(input_title,"Add successfully",Snackbar.LENGTH_SHORT).show();
                        Intent i = new Intent(getApplicationContext(), DashboardActivity.class);
                        startActivity(i);
                    }
                    else{
                        Toast.makeText(getApplicationContext(),"Error: Please try again!",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(),"Error: Please check your date and time formats!",Toast.LENGTH_SHORT).show();

                }
            }
        });
    }


    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.manager_menu, menu);
        return true;
    }

    @Override

    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

